package com.rdxer.api;

import com.rdxer.api.para.IdsStringListPara;
import com.rdxer.api.para.PaperAddQuestionPara;
import com.rdxer.core.entity.Result;
import com.rdxer.core.entity.StatusCode;
import com.rdxer.core.utils.search.SearchPara;
import com.rdxer.dataview.PaperQuestionDV;
import com.rdxer.model.Paper;
import com.rdxer.service.PaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * departmentManagement
 */
@RestController
@CrossOrigin
@RequestMapping("/management/paper")
public class PaperManageApi {

    @Autowired
    private PaperService service;


    @PostMapping("/paperQuestionMvTo/{relationId}/{idx}")
    public Result paperQuestionMvTo(@PathVariable String relationId,@PathVariable Integer idx) {
        service.questionMoveToIndex(relationId,idx);
        return new Result(true, StatusCode.OK, "操作成功");
    }

    @DeleteMapping("/{id}/paperQuestion")
    public Result deletePaperQuestion(@PathVariable String id) {
        service.deletePaperQuestion(id);
        return new Result(true, StatusCode.OK, "操作成功");
    }

    @GetMapping("/{id}/paperQuestion")
    public Result<List<PaperQuestionDV>> paperQuestion(@PathVariable String id) {
        return new Result<List<PaperQuestionDV>>(true, StatusCode.OK, "查询成功", service.findAllQuestion(id));
    }

    /**
     * 查询全部数据
     *
     * @return
     */
    @GetMapping
    public Result<List<Paper>> findAll() {
        return new Result<List<Paper>>(true, StatusCode.OK, "查询成功", service.findAll());
    }

    /**
     * 根据ID查询
     *
     * @param id ID
     * @return
     */
    @GetMapping("/{id}")
    public Result<Paper> findById(@PathVariable String id) {
        return new Result(true, StatusCode.OK, "查询成功", service.findById(id));
    }


    /**
     * @param para
     */
    @PostMapping("/{id}/addQuestion")
    public Result addQuestion(@PathVariable String id, @RequestBody PaperAddQuestionPara para) {
        service.addPaperQuestion(id, para);
        Result<?> result = new Result<>();
        result.setMessage("操作成功");
        return result;
    }

    /**
     * @param para
     */
    @PostMapping("/doEdit")
    public Result save(@RequestBody Paper para) {
        Paper data = service.save(para);
        Result<Paper> result = new Result<>();
        result.setData(data);
        result.setMessage("操作成功");
        return result;
    }

    /**
     * 修改
     *
     * @param department
     */
    @PutMapping("/{id}")
    public Result<Paper> update(@PathVariable String id, @RequestBody Paper department) {

        department.setId(id);

        Paper data = service.update(department);

        Result<Paper> result = new Result<>();
        result.setData(data);
        result.setMessage("修改成功");
        return result;
    }

    /**
     * 删除
     *
     * @param ids
     */
    // @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable IdsStringListPara ids) {
        service.deleteByIds(ids.getIds());
        return new Result(true, StatusCode.OK, "删除成功");
    }

    //	 * 分页+多条件查询
    @PostMapping("/getList")
    public Result<Page<Paper>> search(@Valid @RequestBody SearchPara para) {
        Result<Page<Paper>> result = new Result<>();
        Page<Paper> data = service.search(para);
        result.setData(data);
        return result;
    }

    @PostMapping("/getAll")
    public Result<List<Paper>> searchNoPage(@Valid @RequestBody SearchPara para) {
        Result<List<Paper>> result = new Result<>();
        List<Paper> data = service.searchNoPage(para);
        result.setData(data);
        return result;
    }
}
